﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'auction_userbidproxy';
domain = 'http://img1.chinalaohu.com';
</cfscript>


<!---取消代理--->
<cffunction name="cancel" access="remote" returntype="any" returnFormat="json">
	<cfif isdefined('id') AND isdefined('UUID') AND isdefined('key')>
  	
	<cfscript>
  newkey = hash(id & UUID & 'laohu123','MD5');
  </cfscript>
    
  <cfif key neq newkey>
		<cfscript>
		//KEY不正确
    rj = {"success":"0","errCode":"-1","msg":"key不正确"};
    </cfscript>
  
  <cfelse>    
  
    <cfif len(UUID) lt 32>
    	<!---,"msg":"错误的ID"--->
    	<cfset rj = {"success":"0","errCode":"0","msg":"错误的ID"}>
    <cfelse>
			<!---当前价格--->
      <cfquery datasource="#DB#" name="aidprice">
      select lastprice
      from auction
      where aid = #id#
      </cfquery>
      <cfif aidprice.recordcount eq 0>
      	<!---,"msg":"拍品不存在"--->
      	<cfset rj = {"success":"0","errCode":"-1","msg":"拍品不存在"}>
      <cfelse>
      	
        
				<!---根据UUID查找ID--->
        <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" />
        <cfif uuidgetuid.recordcount neq 1>
        	<!---"msg":"用户ID错误"--->
          <cfset rj = {"success":"0","price":aidprice.lastprice,"errCode":"0","msg":"用户ID错误"}>
        <cfelse>
          <cfquery datasource="#DB#" name="uidaidproxy">
          SELECT bidproxy,uid
          from auction_userbidproxy
          where aid = #id#
          </cfquery>
          <cfinvoke component="app.cfc.credit" method="userCreditLimit" returnvariable="userCreditLimit0" aid="#id#" uid="#uuidgetuid.uid#" />
          <!---是否领先--->
          <cfquery datasource="#DB#" name="uidisLead">
          select uid,is_bid
          from auction_userlastbid
          where aid = #id# AND uid = #uuidgetuid.uid#
          </cfquery>
          <cfscript>
					isLead = 0;
					if (uidisLead.recordcount gt 0){
						isLead = uidisLead.is_bid;
					}
					isProxy = 0;
					credit = userCreditLimit0;
					
			if(isLead eq 1){
				//在客户端中，可用额度减去了当前价（不该减去），在此加回来，避免客户端显示错误
				credit = credit+aidprice.lastprice;
			}
					</cfscript>
          <cfif uidaidproxy.recordcount eq 0 >
          	<!--- ,"msg":"没有代理价" --->
			<cfset rj = {"success":"1","price":aidprice.lastprice,"isLead":isLead,"isProxy":0,"credit":credit,"errCode":"0"}>
          <cfelseif uidaidproxy.uid neq uuidgetuid.uid>
          	<!--- ,"msg":"你的代理价不存在" --->
			<cfset rj = {"success":"1","price":aidprice.lastprice,"isLead":isLead,"isProxy":0,"credit":credit,"errCode":"0"}>
          <cfelse>
            <!---取消你的代理--->
            <cfquery datasource="#DB#">
            DELETE FROM #dbt#
            WHERE aid = #id# and uid = #uuidgetuid.uid#
            </cfquery>
            <!---是否领先--->
            <cfquery datasource="#DB#" name="uidisLead">
            select uid,is_bid
            from auction_userlastbid
            where aid = #id# AND uid = #uuidgetuid.uid#
            </cfquery>
            <cfinvoke component="app.cfc.credit" method="userCreditLimit" returnvariable="userCreditLimit" aid="#id#"  uid="#uuidgetuid.uid#" />          
            <cfscript>
				if (uidisLead.recordcount gt 0){
					isLead = uidisLead.is_bid;
				}
			if(isLead eq 1){
				//在客户端中，可用额度减去了当前价（不该减去），在此加回来，避免客户端显示错误
				userCreditLimit = userCreditLimit+aidprice.lastprice;
			}
            rj = {"success":"1","price":aidprice.lastprice,"isLead":isLead,"credit":userCreditLimit};
            </cfscript>
          </cfif>
        </cfif>
    	</cfif>
    </cfif>
  	
  </cfif>
  
  <cfelse>
  	<cfscript>
		//,"msg":"少参数"
		rj = {"success":"0","errCode":"-1"};
		</cfscript>
  
  </cfif>
	<cfscript>
  rj = createObject("component",'cfjson').encode(rj);
  return rj;
  </cfscript>
  

</cffunction>




</cfcomponent>